package com.eos.freeect.common;

import java.util.List;

public class PaginationSupport {
	// 默认每页记录数
	public final static int PAGESIZE = 30;

	// 每页记录数
	private int pageSize = PAGESIZE;

	// 当前页数据
	@SuppressWarnings("unchecked")
	private List items;

	// 总记录数
	private int totalCount;

	// 每页起始下标数组(页码从0开始)
	private int[] indexes = new int[0];

	// 当前页起始记录下标
	private int startIndex = 0;
	
	// 总页数
	private int pageCount;
	
	// 当前页码(页码从1开始)
	private int currentPage;
	
	// 尾页起始下标
	private int lastIndex;

	@SuppressWarnings("unchecked")
	public PaginationSupport(List items, int totalCount) {
		setPageSize(PAGESIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(0);
	}

	@SuppressWarnings("unchecked")
	public PaginationSupport(List items, int totalCount, int startIndex) {
		setPageSize(PAGESIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	@SuppressWarnings("unchecked")
	public PaginationSupport(List items, int totalCount, int pageSize,
			int startIndex) {
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	@SuppressWarnings("unchecked")
	public List getItems() {
		return items;
	}

	@SuppressWarnings("unchecked")
	public void setItems(List items) {
		this.items = items;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * 设定每页的startIndex数组(其中，页码从0开始)
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount) {
		if (totalCount > 0) {
			this.totalCount = totalCount;
			int count = totalCount / pageSize;
			if (totalCount % pageSize > 0)
				count++;
			indexes = new int[count];
			for (int i = 0; i < count; i++) {
				indexes[i] = pageSize * i;
			}
		} else {
			this.totalCount = 0;
		}
	}

	public int[] getIndexes() {
		return indexes;
	}

	public void setIndexes(int[] indexes) {
		this.indexes = indexes;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		if (totalCount <= 0)
			this.startIndex = 0;
		else if (startIndex >= totalCount)
			this.startIndex = indexes[indexes.length - 1];
		else if (startIndex < 0)
			this.startIndex = 0;
		else {
			this.startIndex = indexes[startIndex / pageSize];
		}
	}

	/**
	 * 得到下一页的起始下标
	 * @return
	 */
	public int getNextIndex() {
		int nextIndex = getStartIndex() + pageSize;
		if (nextIndex >= totalCount)
			return getStartIndex();
		else
			return nextIndex;
	}

	/**
	 * 得到上一页的起始下标
	 * @return
	 */
	public int getPreviousIndex() {
		int previousIndex = getStartIndex() - pageSize;
		if (previousIndex < 0)
			return 0;
		else
			return previousIndex;
	}
	
	/**
	 * 得到总页数
	 * @return
	 */
	public int getPageCount() {
		this.pageCount = getTotalCount()/getPageSize();
		if(getTotalCount()%getPageSize() > 0) {
			this.pageCount++;
		}
		if(this.pageCount == 0) {
			this.pageCount = 1;
		}
		return pageCount;
	}
	
	/**
	 * 得到当前页页码(页码从1开始)
	 * @return
	 */
	public int getCurrentPage() {
		this.currentPage = getStartIndex()/getPageSize() + 1;
		return this.currentPage;
	}
	
	/**
	 * 判断当前页是否为最后一页
	 * @return
	 */
	public boolean isLastPage() {
		return (getTotalCount() - getStartIndex()) <= getPageSize();
	}
	
	/**
	 * 得到最后一页的起始下标
	 * @return
	 */
	public int getLastIndex() {
		if(getTotalCount()%getPageSize() == 0) {
			this.lastIndex = getTotalCount() - getPageSize();
		} else {
			this.lastIndex = getTotalCount() - (getTotalCount()%getPageSize());
		}
		return this.lastIndex;
	}
}
